package com.appkubes.mall.restful.application.user.user.controller;

import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.mall.core.entity.MallSmsCode;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.restful.application.common.base.BaseController;
import com.appkubes.mall.restful.application.common.sms.entity.vo.CodeVO;
import com.appkubes.mall.restful.application.common.sms.service.CodeService;
import com.appkubes.mall.restful.application.user.user.entity.dto.UserAppDTO;
import com.appkubes.mall.restful.application.user.user.entity.dto.UserWxBindDTO;
import com.appkubes.mall.restful.application.user.user.entity.dto.UserWxDTO;
import com.appkubes.mall.restful.application.user.user.entity.vo.LoginByTelVO;
import com.appkubes.mall.restful.application.user.user.entity.vo.LoginByTelWxVO;
import com.appkubes.mall.restful.application.user.user.entity.vo.MallUserWxVO;
import com.appkubes.mall.restful.application.user.user.entity.vo.WxUserBindTelVO;
import com.appkubes.mall.restful.application.user.user.service.UserServiceLogin;
import com.appkubes.mall.restful.application.user.user.service.UserWxAppService;
import com.appkubes.mall.restful.application.user.user.service.impl.UserWxAppServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "用户登录")
@RestController
@RequestMapping("/user/login")
public class UserLoginController extends BaseController {

    @Autowired
    UserServiceLogin userServiceLogin;
    @Autowired
    UserWxAppServiceImpl userWxAppServiceImpl;

    @Autowired
    UserWxAppService userWxAppService;

    @Resource
    CodeService codeService;

    @ApiOperation(value = "通过微信App登录")
    @PostMapping("/wxApp")
    public Response loginByWxApp(@RequestBody @ParamValid MallUserWxVO userWxVO) {
        UserWxDTO userWxDTO = userServiceLogin.loginByWxApp(userWxVO);
        return Response.success(userWxDTO);
    }

    @ApiOperation(value = "绑定手机号")
    @PostMapping("/bindTel")
    public Response bindTel(@RequestBody @ParamValid WxUserBindTelVO wxUserBindTelVO) {
        // 短信验证
        CodeVO codeVO = new CodeVO();
        codeVO.setTel(wxUserBindTelVO.getTel());
        codeVO.setCode(wxUserBindTelVO.getBindCode());
        MallSmsCode mallSmsCode = codeService.getSmsSurvivor(codeVO);
        if (mallSmsCode == null) {
            return Response.error("验证码过期或错误");
        }
        codeService.smsDie(mallSmsCode);

        UserWxBindDTO wxBindDTO = userServiceLogin.bindTel(wxUserBindTelVO);
        return Response.success(wxBindDTO);

    }

    @ApiOperation("手机号登录")
    @PostMapping("/loginByTel")
    public Response<UserAppDTO> loginByTel(@RequestBody @ParamValid LoginByTelVO loginByTelVO) {

        // 短信验证
        CodeVO codeVO = new CodeVO();
        codeVO.setTel(loginByTelVO.getTel());
        codeVO.setCode(loginByTelVO.getSmsCode());
        if(!"13000000000".equals(loginByTelVO.getTel())) {
            MallSmsCode mallSmsCode = codeService.getSmsSurvivor(codeVO);
            if (mallSmsCode == null) {
                return Response.error("验证码过期或错误");
            }
            codeService.smsDie(mallSmsCode);
        }
        MallUser user = userServiceLogin.loginByTel(loginByTelVO);
        UserAppDTO userAppDTO = new UserAppDTO();
        BeanUtils.copyProperties(user, userAppDTO);

        // 显示用户昵称
        userAppDTO.setAvatarUrl(user.getImg());
        userAppDTO.setNickName(user.getName());
        userAppDTO.setToken(user.getRegisterType());

        return Response.success(userAppDTO);
    }


    @ApiOperation("微信绑定手机登录")
    @PostMapping("/loginByTelWx")
    public Response<UserAppDTO> loginByTelWx(@RequestBody @ParamValid LoginByTelWxVO loginByTelWxVO) {
        MallUser user = userServiceLogin.loginByTelWx(loginByTelWxVO);
        UserAppDTO userAppDTO = new UserAppDTO();
        BeanUtils.copyProperties(user, userAppDTO);

        // 显示用户昵称
        userAppDTO.setAvatarUrl(user.getImg());
        userAppDTO.setNickName(user.getName());

        return Response.success(userAppDTO);
    }


}
